﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PhotoPreparer
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter path: ");
            string sPath = Console.ReadLine();
            int iSize = 1600;
            try
            {
                var files = Directory.GetFiles(sPath);
                foreach (string sFile in files)
                {
                    ResizeImage(sFile, iSize);
                    Console.WriteLine(sFile);
                }
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("Error occured: {0}", ex.Message));
            }
        }

        private static void ResizeImage(string sFilename, int iSize)
        {
            int iWidth = 0;
            int iHeight = 0;

            Image oImg = System.Drawing.Image.FromFile(sFilename);
            if (iSize > Math.Max(oImg.Width, oImg.Height))
                iSize = Math.Max(oImg.Width, oImg.Height);

            if (oImg.Width > oImg.Height)
            {
                iWidth = iSize;
                iHeight = (iWidth * oImg.Height) / oImg.Width;
            }
            else
            {
                iHeight = iSize;
                iWidth = (iHeight * oImg.Width) / oImg.Height;
            }

            string sNewFilename = string.Format("{0}_{1}.{2}", sFilename.Substring(0, sFilename.LastIndexOf(".")), iSize, "jpg");

            Bitmap oTarget = new Bitmap(iWidth, iHeight);
            Graphics oThumbnail = Graphics.FromImage(oTarget);
            oThumbnail.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
            oThumbnail.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            oThumbnail.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
            oThumbnail.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            oThumbnail.DrawImage(oImg, new Rectangle(0, 0, iWidth, iHeight));
            oTarget.Save(sNewFilename, System.Drawing.Imaging.ImageFormat.Jpeg);
            // clean up
            oImg.Dispose();
            oTarget.Dispose();
            oThumbnail.Dispose();

            // clean up the image file handle
            oImg.Dispose();
        }
    }
}
